/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.AccountDAL;
import client.dal.CustomerDAL;
import client.dal.CvDAL;
import client.dal.FeedBackDAL;
import client.dal.WorkerDAL;
import client.entity.Account;
import client.entity.Customer;
import client.entity.Cv;
import client.entity.FeedbackSystem;
import client.entity.Worker;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class WorkerProfile implements Serializable {

    private FeedbackSystem fs;
    private Worker worker;
    private String gender_temp;
    private ArrayList<Cv> listCv;
    private int accId;

    public int getAccId() {
        return accId;
    }

    public void setAccId(int accId) {
        this.accId = accId;
    }

    public FeedbackSystem getFs() {
        if (fs == null) {
            fs = new FeedbackSystem();
        }
        return fs;
    }

    public void setFs(FeedbackSystem fs) {
        this.fs = fs;
    }

    public ArrayList<Cv> getListCv() {
        if (listCv == null) {
            listCv = new ArrayList<Cv>();
        }
        return listCv;
    }

    public void setListCv(ArrayList<Cv> listCv) {
        this.listCv = listCv;
    }

    public String getGender_temp() {
        return gender_temp;
    }

    public void setGender_temp(String gender_temp) {
        this.gender_temp = gender_temp;
    }

    public Worker getWorker() {
        if (worker == null) {
            Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
            accId = ac.getId();
            worker = new WorkerDAL().loadWorkerByAccountID(accId);
            if (worker.getGender()) {
                gender_temp = "Male";
            } else {
                gender_temp = "Female";
            }
            listCv = new CvDAL().loadCvByWorkerID(worker.getId());
        }
        return worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }

    /**
     * Creates a new instance of WorkerProfile
     */
    public WorkerProfile() {
    }

    public String editCV(int cvID) {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerEditCV");
        return "workerCVEdit.xhtml?cvID=" + cvID + "&faces-redirect=true";
    }

    public String deleteCV(Cv cv) {
        int cvId = cv.getId();
//        new CvDAL().deleteEdu(cvId);
//        new CvDAL().deleteEmpHis(cvId);
//        new CvDAL().deleteCvCateByRecId(cvId);
//        new CvDAL().deleteCvLocByRecId(cvId);
        new CvDAL().deleteCv(cv);
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }
    
    public String createCv()
    {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerCreateCV");
        return "createCVStep1.xhtml?accId="+accId+"&faces-redirect=true";
    }

    public String activeCv(Cv cv) {
        cv.setActive(Boolean.TRUE);
        new CvDAL().deactiveCv();
        new CvDAL().activeCv(cv);
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }

    public String getParam(String param) {

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }

    public String editProfile() {
        return "workerEditInfo.xhtml?faces-redirect=true";
    }

    public String sendFeedBack() {
        fs.setWorker(worker);
        new FeedBackDAL().insertFeedBack(fs);
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("workerProfile");
        return "workerProfile.xhtml?faces-redirect=true";
    }
}
